package com.csc.finalproject.service;

import java.sql.SQLDataException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csc.finalproject.dao.AccountInfoDAO;
import com.csc.finalproject.dao.StateDAO;
import com.csc.finalproject.domain.AccountInfo;
import com.csc.finalproject.domain.State;

@Service("accountInfoService")
public class AccountInfoServiceImpl implements AccountInfoService {
	@Autowired
	AccountInfoDAO accountDAO;

	@Autowired
	StateDAO stateDAO;

	@Override
	public List<AccountInfo> getAccountInfos() {
		// TODO Auto-generated method stub
		List<AccountInfo> list = accountDAO.getAll();
		return list;
	}

	@Override
	public AccountInfo getAccountInfo(int id) {
		// TODO Auto-generated method stub
		AccountInfo acc = accountDAO.find(id);
		return acc;

	}

	@Override
	public void addAccountInfo(String accountNumber, String indentifyCard,
			String firstName, String lastName, String midName, String phone1,
			String phone2, String email1, String email2, int stateId) {
		AccountInfo account = new AccountInfo(accountNumber, indentifyCard,
				firstName, lastName, midName, phone1, phone2, email1, email2);

		State state = stateDAO.find(stateId);
		account.setState(state);
		accountDAO.create(account);
	}

	@Override
	public void editAccountInfo(int accountId, String accountNumber,
			String indentifyCard, String firstName, String lastName,
			String midName, String phone1, String phone2, String email1,
			String email2, int stateId) {
		AccountInfo account = new AccountInfo(accountId, accountNumber,
				indentifyCard, firstName, lastName, midName, phone1, phone2,
				email1, email2);
		State state = stateDAO.find(stateId);
		account.setState(state);
		accountDAO.update(account);
	}

	@Override
	public List<AccountInfo> getAccountsByState(int stateId) {
		return accountDAO.getAccountsByState(stateId);
	}

	@Override
	public boolean transferState(String accountId, String stateId) {
		boolean valueReturn;
		try
		{
		    AccountInfo account = accountDAO.find(Integer.parseInt(accountId));
		    State state = stateDAO.find(Integer.parseInt(stateId));
			account.setState(state);
			accountDAO.update(account);
			valueReturn = true;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			valueReturn = false;
		}
		return valueReturn;
	}

	@Override
	public boolean removeAccountInfo(String accountId) {
		boolean valueReturn;
		try
		{
			accountDAO.delete(Integer.parseInt(accountId));
			valueReturn = true;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			valueReturn = false;
		}
		return valueReturn;
	}
	
	@Override
	public List<AccountInfo> search(String accountNumber) {
		return accountDAO.search(accountNumber);
	}
	

	@Override
	public List<AccountInfo> searchAdvance(String accountNumber,
			String identifyCard, String firstName, String lastName,
			String phone, String stateId) {

		Map<String, String> map = new HashMap<String, String>();
		map.put("accountNumber", accountNumber);
		map.put("identifyCard", identifyCard);
		map.put("firstName", firstName);
		map.put("lastName", lastName);
		map.put("phone", phone);
		map.put("state", stateId.toString());

		return accountDAO.searchAdvance(map);
	}
}
